/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lyy.demo.openapi.demo.controller;


import com.lyy.demo.openapi.demo.domain.UserReq;
import com.lyy.demo.openapi.demo.domain.UserRes;
import com.lyy.demo.openapi.demo.enums.SexEnum;
import com.lyy.demo.openapi.demo.enums.UserTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "测试swagger枚举")
@RestController
@RequestMapping("/enum")
public class EnumController {

    @PostMapping("/postRequestBody")
    @Operation(summary = "POST接口请求体为实体类内枚举")
    public void postRequestBody(@RequestBody UserReq req) {
    }

    /**
     * ！！！！这是一个反例接口:@Parameters不会正常渲染枚举类型,请改用@Schema(description = "xxx")
     * <pre>参见:</pre>
     * {@link EnumController#getRequestParamIsEnumNum(SexEnum)}的用法
     * @param userType
     */
    @GetMapping("/getRequestParamIsEnumStr")
    @Operation(summary = "get接口请求参数为直接枚举字符串")
    //@Parameters({  //不推荐，因为不能正常渲染自定义枚举策略
    //        @Parameter(name = "userType", description = "用户类型", required = true, schema = @Schema(implementation = UserTypeEnum.class))
    //})
    // 推荐以下 @Schema(description = "用户类型") 方式来定义query方式的参数文档
    public void getRequestParamIsEnumStr(@RequestParam(name = "userType", required = true) @Schema(description = "用户类型") UserTypeEnum userType) {
    }

    @GetMapping("/getRequestPathVariableIsEnumStr/{userType}")
    @Operation(summary = "get接口请求路径参数为直接枚举字符串")
    // 推荐以下 @Schema(description = "用户类型") 方式来定义path方式的参数文档
    public void getRequestPathVariableIsEnumStr(@PathVariable(name = "userType") @Schema(description = "用户类型") UserTypeEnum userType) {
    }

    @GetMapping("/getRequestParamIsEnumNum")
    @Operation(summary = "get接口请求参数为直接枚举数字")
    public void getRequestParamIsEnumNum(@RequestParam(name = "sexEnum", required = true) @Schema(description = "用户类型") SexEnum sexEnum) {
    }

    @GetMapping("/responseBody")
    @Operation(summary = "响应体内为实体类枚举")
    public UserRes responseBody() {
        return new UserRes();
    }

}
